package cn.dduan.fool.design.pattern.behavior.mediator;


import java.util.HashMap;
import java.util.Map;

/**
 * 中介者模式
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
public class MediatorPattern {
    public static void main(String[] args) {
        Mediator mediator = new MediatorImpl();

        Colleague colleagueA = new ColleagueImpl("小花", mediator);
        Colleague colleagueB = new ColleagueImpl("小明", mediator);
        Colleague colleagueC = new ColleagueImpl("小绿", mediator);
        Colleague colleagueD = new ColleagueImpl("小蓝", mediator);

        mediator.addColleague(colleagueA);
        mediator.addColleague(colleagueB);
        mediator.addColleague(colleagueC);
        mediator.addColleague(colleagueD);

        colleagueA.contactColleague("大家好", colleagueB.getName(), colleagueC.getName(), colleagueD.getName());
    }
}

/**
 * 抽象同事类
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
abstract class Colleague {

    private Mediator mediator;

    private String name;

    Colleague(String name, Mediator mediator) {
        this.name = name;
        this.mediator = mediator;
    }

    public Mediator getMediator() {
        return mediator;
    }

    public String getName() {
        return name;
    }

    /**
     * 得到其他同事发来的信息
     *
     * @param message
     * @param messageFormName
     * @return void
     * @author dduan
     * @version 1.0
     * @date 2023/4/10
     */
    public abstract void getMessage(String message, String messageFormName);

    /**
     * 与其他同事通信
     *
     * @param message
     * @param toColleagueName
     * @return void
     * @author dduan
     * @version 1.0
     * @date 2023/4/10
     */
    public abstract void contactColleague(String message, String... toColleagueName);
}

/**
 * 具体同事类
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class ColleagueImpl extends Colleague {

    ColleagueImpl(String name, Mediator mediator) {
        super(name, mediator);
    }

    @Override
    public void getMessage(String message, String messageFormName) {
        System.out.println("我" + this.getName() + " 被" + messageFormName + " 联系了， " + "信息为:" + message);
    }

    @Override
    public void contactColleague(String message, String... toColleagueNames) {
        System.out.print("我 " + this.getName() + " 向 ");
        for (String toColleagueName : toColleagueNames) {
            System.out.print(toColleagueName + ",");
        }
        System.out.println(" 发 " + message + " 信息");
        this.getMediator().contact(message, this, toColleagueNames);
    }
}

/**
 * 抽象中介者
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
interface Mediator {

    /**
     * 中介者来使各同事之间的联系
     *
     * @param content          交流内容
     * @param toColleagueNames 被联系者的名字
     */
    void contact(String content, Colleague colleague, String... toColleagueNames);

    /**
     * 添加同事
     *
     * @param colleague 被添加的同事
     */
    void addColleague(Colleague colleague);
}

/**
 * 具体中介者
 *
 * @author dduan
 * @version 1.0
 * @date 2023/4/10
 */
class MediatorImpl implements Mediator {

    // 维护并知道所有同事。
    private Map<String, Colleague> colleagueMap = new HashMap<>();


    @Override
    public void contact(String message, Colleague colleagueForm, String... toColleagueNames) {
        for (String name : toColleagueNames) {
            Colleague colleague = colleagueMap.get(name);
            colleague.getMessage(message, colleagueForm.getName());
        }
    }

    @Override
    public void addColleague(Colleague colleague) {
        this.colleagueMap.put(colleague.getName(), colleague);
    }
}
